package edu.rakovic.elearning.pages;

import org.apache.tapestry5.Block;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.rakovic.elearning.beans.ContentBean;
import edu.rakovic.elearning.enums.Role;
import edu.rakovic.elearning.interfaces.Content;
import edu.rakovic.elearning.interfaces.Course;
import edu.rakovic.elearning.interfaces.User;
import edu.rakovic.elearning.service.Contents;

public class AddContent {
	
	@Property
	@Inject
	private Block add;
	
	@Property
	@Inject
	private Block congratulations;
	
	@Property
	private String singleUnit;
	
	@Persist
	private Course course;
	
	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	@Inject
	private Contents contents;
	
	@OnEvent (value="submit", component="contentInputForm")
	Object onFromSubmit(){
	
		Content content = new ContentBean();
		content.setSingleUnit(singleUnit);
		content.setCourse(course);
		contents.save(content);
		return congratulations;	

	}
	
	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		if (getUser().getRole()!= Role.TEACHER) return Index.class;
		return null;
	}

}
